package ro.imc.andrei.visual;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ro.imc.andrei.beans.Group;
import ro.imc.andrei.ums.UMSDB;

public class AddGroupFrame extends JFrame {

	// Labels
	private JLabel groupIdLabel = new JLabel("Group ID");
	private JLabel groupNameLabel = new JLabel("Group Name");
	private JLabel yearOfStudyLabel = new JLabel("Year Of Study");
	private JLabel fieldOfStudyLabel = new JLabel("Field Of Study");

	// Text Fields
	private JTextField groupIdTextField = new JTextField();
	private JTextField groupNameTextField = new JTextField();
	private JTextField fieldOfStudyTextField = new JTextField();

	// Combo Box
	private JComboBox<String> yearOfStudyComboBox = new JComboBox<String>();

	// Button
	private JButton saveButton = new JButton("Save");
	private JButton cancelButton = new JButton("Cancel");

	// Panels
	private JPanel mainPanel = new JPanel();

	private JFrame parent;

	private static int WITDH = 400;
	private static int HEIGTH = 200;

	public AddGroupFrame(JFrame parent) {
		this.parent = parent;
		initFrame();
	}

	private void initFrame() {
		// Init Combo Box
		yearOfStudyComboBox.removeAllItems();
		yearOfStudyComboBox.addItem("1");
		yearOfStudyComboBox.addItem("2");
		yearOfStudyComboBox.addItem("3");
		yearOfStudyComboBox.addItem("4");
		yearOfStudyComboBox.addItem("5");
		yearOfStudyComboBox.addItem("6");

		// Init Frame
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setSize(WITDH, HEIGTH);
		this.setTitle("Add Group");

		// Add Panel to Frame and set Panel layout
		this.add(mainPanel);
		mainPanel.setLayout(new GridLayout(0, 2));

		// Add labels, text fields, and button
		mainPanel.add(groupNameLabel);
		mainPanel.add(groupNameTextField);

		mainPanel.add(groupIdLabel);
		mainPanel.add(groupIdTextField);

		mainPanel.add(yearOfStudyLabel);
		mainPanel.add(yearOfStudyComboBox);

		mainPanel.add(fieldOfStudyLabel);
		mainPanel.add(fieldOfStudyTextField);

		mainPanel.add(saveButton);
		mainPanel.add(cancelButton);
		
		saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonSaveActionPerformed(evt);
            }
        });
		
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
					jButtonCancelActionPerformed(evt);
            }
        });
	}
	
	private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt){
		parent.setVisible(true);
		this.dispose();
	}
	
	private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt){
		try{
			Group group = new Group();
			
			group.setGroupName(groupNameTextField.getText());
			group.setGroupId(Integer.parseInt(groupIdTextField.getText()));
			group.setYearOfStudy(Integer.parseInt(yearOfStudyComboBox.getSelectedItem().toString()));
			group.setFieldOfStudy(fieldOfStudyTextField.getText());
			
			UMSDB db = UMSDB.getInstance();
			
			String[] fields = group.getFields();
			String[] values = group.getValues();
			
			db.insertMethod("groups", fields, values);
			
		}
		catch(NumberFormatException nfe){
			JOptionPane pane =
			new JOptionPane("Group ID Cannot Be Made of Letters!", JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION);
			JDialog dialog = pane.createDialog(null, "Error!");
			dialog.setVisible(true);
		}
		catch(Exception e){
			e.printStackTrace();
			JOptionPane pane =
			new JOptionPane(e.getMessage(), JOptionPane.ERROR_MESSAGE, JOptionPane.DEFAULT_OPTION);
			JDialog dialog = pane.createDialog(null, "Error!");
			dialog.setVisible(true);
		}
	}
}
